package com.llb666.shop.order.infrastructure.persistence;

import com.llb666.common.core.enums.OrderStatusEnum;
import com.llb666.shop.order.domain.entity.OrderStatus;
import com.llb666.shop.order.domain.repository.OrderStatusRepository;
import com.llb666.shop.order.infrastructure.conver.OrderStatusCvt;
import com.llb666.shop.order.infrastructure.mapper.OrderStatusMapper;
import com.llb666.shop.order.infrastructure.po.OrderStatusPo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

/**
 * 订单状态仓储层.
 *
 * @author llb666
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class OrderStatusRepositoryImpl implements OrderStatusRepository {

    private final OrderStatusMapper orderStatusMapper;

    /**
     * 根据订单id,修改订单状态表的留言时间.
     *
     * @param orderId 订单id
     */
    @Override
    public void updateOrderStatus(String orderId) {
        OrderStatusPo orderStatus = new OrderStatusPo();
        orderStatus.setOrderId(orderId);
        orderStatus.setCommentTime(new Date());
        orderStatusMapper.updateByPrimaryKeySelective(orderStatus);
    }

    /**
     * 内部接口修改订单状态.
     *
     * @param merchantOrderId 订单Id
     * @param orderStatus          订单状态
     */
    @Override
    public void updateOrderStatusByType(String merchantOrderId, Integer orderStatus) {
        OrderStatusPo paidStatus = new OrderStatusPo();
        paidStatus.setOrderId(merchantOrderId);
        paidStatus.setOrderStatus(orderStatus);
        paidStatus.setPayTime(new Date());
        orderStatusMapper.updateByPrimaryKeySelective(paidStatus);
    }

    /**
     * 内部接口获取订单信息.
     *
     * @param orderId 订单Id
     * @return 订单状态
     */
    @Override
    public OrderStatus queryOrderStatusInfo(String orderId) {
        return OrderStatusCvt.INSTANCE.po2do(orderStatusMapper.selectByPrimaryKey(orderId));
    }

    /**
     * 新增订单状态.
     *
     * @param waitPayOrderStatus 待支付订单状态实体
     */
    @Override
    public void insert(OrderStatus waitPayOrderStatus) {
        orderStatusMapper.insert(OrderStatusCvt.INSTANCE.do2po(waitPayOrderStatus));
    }


    /**
     * 超时订单，更新待支付的订单为close.
     *
     * @param orderId 订单Id
     * @param status  订单状态:close
     */
    @Override
    public void updateOrderStatusByWaitPay(String orderId, Integer status) {
        // 对象待修改的字段.
        OrderStatusPo paidStatus = new OrderStatusPo();
        paidStatus.setOrderId(orderId);
        paidStatus.setOrderStatus(status);
        paidStatus.setPayTime(new Date());
        // 更新筛选条件.
        Example example = new Example(OrderStatusPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId", orderId);
        criteria.andEqualTo("orderStatus", OrderStatusEnum.WAIT_PAY.type);
        orderStatusMapper.updateByExampleSelective(paidStatus, example);
    }

}
